
%%% produces images with the different settings


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for lim_angl=[35 160] ;
for id=[1 2];
for noise=[0 0.03];
for mode=[0];
image='phantom';
maxit=2;
for iniG=3
switch iniG
    case 1
        initialGuessStr='sfbp';
    case 2
        initialGuessStr='fbp';
    case 3
        initialGuessStr='zero';
end
constThreshValue=10e-5;
imgSize=512;

method='gradient';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


angles=(-lim_angl/2):(lim_angl/2);
truncate=true;

switch mode
    case 0
        mode='';
    case 1
        mode='smooth_tst_';
end

switch id
    case 1
        identifier=['limitedAngle_outerFading_' num2str(lim_angl)];
    case 2
        identifier=['limitedAngle_innerFading_' num2str(lim_angl)];
    case 3
        identifier=['limitedAngle_fixedFading_3_' num2str(2/3*lim_angl) '_' num2str(lim_angl)];
    case 4
        identifier=['limitedAngle_fixedFading_3_' num2str(lim_angl) '_' num2str(1.1*lim_angl)];
    case 5
        identifier=['mycandes'];

end

if(~strcmp(mode,''))
    identifier={identifier,[0 1],[mode num2str(2/3*lim_angl) '_' num2str(lim_angl)]};      
end

switch image
    case 'phantom'
        img=phantom(imgSize);
    case 'lena'
        img=double(imread('lena512.pgm'))/255;
        sd=512/imgSize;
        if(sd>1)
            img=sample_down(img,sd);
        end
    case 'ball'
        img=get_image('ball',[imgSize,imgSize],'mid','srp30');
end

Y=radon(img,angles);

if(noise>0)
    Y=addNoise(Y,noise);
end

switch method
    case 'gradient'
        out1=my_iterative_soft_thresholding(Y,angles,imgSize,maxit,initialGuessStr,constThreshValue,identifier);
    case 'cg'
        out1=my_iterative_cg(Y,angles,imgSize,maxit,initialGuessStr,constThreshValue,identifier);
    case 'cgV2'
        out1=my_iterative_cgV2(Y,angles,imgSize,maxit,initialGuessStr,constThreshValue,identifier);
    case 'cgV3'
        out1=my_iterative_cgV3(Y,angles,imgSize,maxit,initialGuessStr,constThreshValue,identifier);
end
min_x=find(out1.error==min(out1.error));

% normalize=true;
% if(normalize)
%     for i=1:length(out1.error)
%         out1.iterates(:,:,1)=out1.iterates(:,:,1)-min(out1.iterates(:,:,1)).*ones(size(out1.iterates(:,:,1)));
%         out1.iterates(:,:,1)=out1.iterates(:,:,1)/max(out1.iterates(:,:,1))*255;
%     end
% end

if(iscell(identifier))
   identifier=identifier{1};
end

%imwrite(out1.iterates(:,:,min_x),['./recImg/' image '_' identifier '_' initialGuessStr '_noi' num2str(noise) '_it' num2str(min_x)  '.png'],'PNG');%['./recImg/' image '_' identifier '_' initialGuessStr '_noi' num2str(noise) 'it' min_x  '.png'],'PNG');
success=0;
tries=0;
while(~success && tries<10)

try    
tries=tries+1;
imwrite(out1.iterates(:,:,1),['./recImg/' image '_' identifier '_' method '_'  initialGuessStr '_noi' num2str(noise) '_' mode  '.tif'],'tif','writemode','overwrite');%['./recImg/' image '_' identifier '_' initialGuessStr '_noi' num2str(noise) 'it' min_x  '.png'],'PNG');

for i=2:length(out1.error)
    pause(eps);
    imwrite(out1.iterates(:,:,i),['./recImg/' image '_' identifier '_' method '_'  initialGuessStr '_noi' num2str(noise) '_' mode  '.tif'],'tif','writemode','append');
end

% save(['./recImg/' image '_' identifier '_' method '_' initialGuessStr '_noi' num2str(noise) '_' mode  '.mat'],'out1');
% rec=out1.rec;
% save(['./recImg/' image '_' identifier '_' method '_' initialGuessStr '_noi' num2str(noise) '_' mode  '_rec.mat'],'rec');

success=1;
fprintf(['image ' image '_' identifier '_' method '_' initialGuessStr '_noi' num2str(noise) '_' mode '.tif' ' succesfully saved\n']);

catch err
 fprintf(['err ' num2str(tries)]); 
 pause(eps);
 %if(tries==10000)
 %   rethrow(err); 
 %end
end


end


end
end
end
end
end
